/**
 * 回购信息，不上生产
 * @author gf
 */

import { PageWrapper } from "@/components/PageWrapper";
import LittleTitle from "@/components/Title/littleTitle";
import CusDatePicker from "@/components/Picker/datePicker";
import dayjs from 'dayjs';

import { findAll, queryFundBasicInfoList } from "./service";

import { useState, useEffect } from "react";
import STable from "@/components/Table";
import SSelect from "@/components/Select";
import { FlexDiv } from "@/components/FOF/DoubleInput";
import { NewText } from "../RidingReturns";
import { Select } from "antd";

const TableColumns = [
  {
    title: "基金代码",
    key: "innercode",
    dataIndex: "innercode",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "回购日期",
    key: "repdate",
    dataIndex: "repdate",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "序号",
    key: "serialnumber",
    dataIndex: "serialnumber",
    render: (text) => (text === 0 || text) ? text : "-",
  },

  {
    title: "流水号",
    key: "flownumber",
    dataIndex: "flownumber",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "基金编号",
    key: "fundcode",
    dataIndex: "fundcode",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "基金名称",
    key: "chiname",
    dataIndex: "chiname",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "资产单元序号",
    key: "assetunitnum",
    dataIndex: "assetunitnum",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  //   {
  //     title: "资产单元名称",
  //     key: "assetunitname",
  //     dataIndex: "assetunitname",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  {
    title: "资产单元编号",
    key: "assetunitcode",
    dataIndex: "assetunitcode",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  //   {
  //     title: "组合编号",
  //     key: "portfoliocode",
  //     dataIndex: "portfoliocode",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  //   {
  //     title: "组合名称",
  //     key: "portfolioname",
  //     dataIndex: "portfolioname",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  {
    title: "证券名称",
    key: "secuname",
    dataIndex: "secuname",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "回购代码",
    key: "repcode",
    dataIndex: "repcode",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "实际回购代码",
    key: "actualrepcode",
    dataIndex: "actualrepcode",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  // {
  //   title: "证券类别",
  //   key: "secucategory",
  //   dataIndex: "secucategory",
  //   render: (text) => (text === 0 || text) ? text : "-",
  // },
  {
    title: "委托方向",
    key: "entrustdirection",
    dataIndex: "entrustdirection",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  // {
  //   title: "委托方向编码",
  //   key: "entrustdirectioncode",
  //   dataIndex: "entrustdirectioncode",
  //   render: (text) => (text === 0 || text) ? text : "-",
  // },
  {
    title: "数量",
    key: "volume",
    dataIndex: "volume",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "平均利率(%)",
    key: "avginterestrate",
    dataIndex: "avginterestrate",
    render: (text) => (text === 0 || text) ? text?.toFixed(4) : "-",
  },
  {
    title: "实际利率(%)",
    key: "actualinterestrate",
    dataIndex: "actualinterestrate",
    render: (text) => (text === 0 || text) ? text?.toFixed(4) : "-",
  },
  {
    title: "净资金额",
    key: "netcapital",
    dataIndex: "netcapital",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "返回金额",
    key: "returncapital",
    dataIndex: "returncapital",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "利润",
    key: "profit",
    dataIndex: "profit",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "额外利润",
    key: "extraprofit",
    dataIndex: "extraprofit",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "交易市场",
    key: "exchange",
    dataIndex: "exchange",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  // {
  //   title: "清算速度",
  //   key: "settlerate",
  //   dataIndex: "settlerate",
  //   render: (text) => (text === 0 || text) ? text : "-",
  // },
  //   {
  //     title: "法定购回日期",
  //     key: "authrepdate",
  //     dataIndex: "authrepdate",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  //   {
  //     title: "实际购回日期",
  //     key: "actualrepdate",
  //     dataIndex: "actualrepdate",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  //   {
  //     title: "购回交割日期",
  //     key: "deliveryrepdate",
  //     dataIndex: "deliveryrepdate",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  //   {
  //     title: "购回处理日期",
  //     key: "processrepdate",
  //     dataIndex: "processrepdate",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  {
    title: "实际占款天数",
    key: "actualholdays",
    dataIndex: "actualholdays",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "交易对手",
    key: "counterparty",
    dataIndex: "counterparty",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "计提利息",
    key: "calculatedinterest",
    dataIndex: "calculatedinterest",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  {
    title: "总应计利息",
    key: "accruinterest",
    dataIndex: "accruinterest",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  // {
  //   title: "首次结算方式",
  //   key: "initialsettletype",
  //   dataIndex: "initialsettletype",
  //   render: (text) => (text === 0 || text) ? text : "-",
  // },
  // {
  //   title: "到期结算方式",
  //   key: "endsettletype",
  //   dataIndex: "endsettletype",
  //   render: (text) => (text === 0 || text) ? text : "-",
  // },
  //   {
  //     title: "托管机构",
  //     key: "trusteename",
  //     dataIndex: "trusteename",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  {
    title: "交易员",
    key: "trader",
    dataIndex: "trader",
    render: (text) => (text === 0 || text) ? text : "-",
  },
  //   {
  //     title: "股东代码",
  //     key: "shareholdercode",
  //     dataIndex: "shareholdercode",
  //     render: (text) => (text === 0 || tex)t) ? text: "-,
  //   },
  //   {
  //     title: "委托方向代码",
  //     key: "entrustDirectionJYSC",
  //     dataIndex: "entrustDirectionJYSC",
  //     render: (text) => text || "-",
  //   },
  //   {
  //     title: "到期购回交易员",
  //     key: "maturityRepurchaseTrader",
  //     dataIndex: "maturityRepurchaseTrader",
  //     render: (text) => text || "-",
  //   },
  //   {
  //     title: "编辑用户",
  //     key: "editorID",
  //     dataIndex: "editorID",
  //     render: (text) => text || "-",
  //   },
  //   {
  //     title: "数据产生时间",
  //     key: "reacteTime",
  //     dataIndex: "reateTime",
  //     render: (text) => text || "-",
  //   },
  //   {
  //     title: "最后修改时间",
  //     key: "updateTime",
  //     dataIndex: "updateTime",
  //     render: (text) => text || "-",
  //   },
  //   {
  //     title: "数据来源",
  //     key: "dataSrc",
  //     dataIndex: "dataSrc",
  //     render: (text) => text || "-",
  //   },
  //   {
  //     title: "源库",
  //     key: "orisys",
  //     dataIndex: "orisys",
  //     render: (text) => text || "-",
  //   },
];

export function RepoInfo() {
  const [start, setStart] = useState(dayjs(Date.now()).subtract(2, "years"));
  const [end, setEnd] = useState(dayjs(Date.now()).subtract(1, "days"));
  const [tableList, setTableList] = useState({}); // 回购信息
  const [fundList, setFundList] = useState([]);
  const [innerCode, setInnerCode] = useState();

  // 查询回购信息
  const handleQueryAll = (params) => {
    findAll({
      current: 1,
      size: 10,
      start: dayjs(start).format("YYYY-MM-DD"),
      end: dayjs(end).format("YYYY-MM-DD"),
      innerCode,
      ...params,
    }).then((res) => {
      if (res?.code === "0") {
        setTableList(res.data);
      } else {
        setTableList({});
      }
    });
  };

  const onChangePage = (pagination) => {
    const { current, pageSize } = pagination;
    handleQueryAll({ current, size: pageSize });
  };

  useEffect(() => {
    //获取默认基金的innerCode
    queryFundBasicInfoList({
      chiname: "东海鑫享66个月定开",
    }).then((res) => {
      if (res?.code === "0" && res?.data?.length > 0) {
        setInnerCode(res?.data[0]?.innercode);
      }
    });
    //获取东海基金列表
    queryFundBasicInfoList({}).then((res) => {
      console.log(res);
      if (res?.code === "0" && res?.data?.length > 0) {
        setFundList(res?.data);
      } else {
        setFundList([]);
      }
    });
  }, []);

  useEffect(() => {
    if (start && end && innerCode) {
      handleQueryAll({
        start: dayjs(start).format("YYYY-MM-DD"),
        end: dayjs(end).format("YYYY-MM-DD"),
      });
    }
  }, [start, end, innerCode]);

  // 保存查询条件
  const handleSaveParams = (e: any, type: string) => {
    if (type === "start") {
      setStart(e);
    } else {
      setEnd(e);
    }
  };

  const onChangeFund = (e, option: any) => {
    setInnerCode(e);
  };
  return (
    <PageWrapper style={{ padding: "16px 32px 32px" }}>
      <div style={{ display: "flex", justifyContent: "space-between" }}>
        <LittleTitle title="回购信息" />
        <FlexDiv>
          <FlexDiv style={{ marginRight: 24 }}>
            <NewText style={{ marginLeft: 0, marginRight: 8 }}>
              基金产品
            </NewText>
            <SSelect
              suffixIcon={<div className="findfit_icon_arrow " />}
              value={innerCode}
              onChange={onChangeFund}
              // iserror={errorList.includes("lineType")}
              style={{ width: 260 }}
            >
              {fundList?.map((v) => (
                <Select.Option key={v?.innercode} value={v?.innercode}>
                  {v?.chiname}
                </Select.Option>
              ))}
            </SSelect>
          </FlexDiv>
          <CusDatePicker
            label="时间范围"
            startValue={start}
            endValue={end}
            idName1="riding-date"
            onChange={handleSaveParams}
            disabledDate={false}
            format={"YYYY/MM/DD"}
          />
        </FlexDiv>
      </div>
      <STable
        columns={TableColumns}
        style={{ whiteSpace: "nowrap" }}
        scroll={{ x: true }}
        dataSource={{
          ...tableList,
          pageSizeOptions: [10, 50, 100],
        }}
        onChange={onChangePage}
      />
    </PageWrapper>
  );
}
